package com.wannengbang.cloudleader.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.utils.FastClickUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 划拨或分配给门店
 */
public class AllotAgentDialog extends DialogFragment {

    @BindView(R.id.tv_select_name)
    TextView tvSelectName;
    @BindView(R.id.tv_agent_name)
    TextView tvAgentName;
    @BindView(R.id.ll_select_agent)
    LinearLayout llSelectAgent;
    @BindView(R.id.tv_current_num)
    EditText tvCurrentNum;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private Dialog dialog;
    private OnCallBackListener onCallBackListener;
    private Activity mActivity;
    private String agent_id = "";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_allot_agent, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        return view;
    }

    public static AllotAgentDialog newInstance() {
        Bundle args = new Bundle();
        AllotAgentDialog fragment = new AllotAgentDialog();
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    //    初始化请求
    private void initView() {

    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.tv_commit, R.id.ll_select_agent})
    public void onViewClicked(View view) {
        if (FastClickUtils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_select_agent:
                ChooseAgentDialog dialog = ChooseAgentDialog.newInstance("1", "1");
                dialog.show(getFragmentManager(), "dialog");
                dialog.setOnCallBackListener((name, chooseId) -> {
                    agent_id = chooseId;
                    tvAgentName.setText(name);
                });
                break;
            case R.id.tv_commit:
                String agentName = tvAgentName.getText().toString();
                if (TextUtils.isEmpty(agentName)) {
                    ToastUtil.showShort("请选择合伙人");
                    return;
                }
                String num = tvCurrentNum.getText().toString();
                if (TextUtils.isEmpty(num) || Integer.parseInt(num) <= 0) {
                    ToastUtil.showShort("请输入分配数量");
                    return;
                }

                new HomePageModelImpl().requestTeamRewardSet(agent_id, num, new DataCallBack<BaseResponseBean>() {
                    @Override
                    public void onSuccessful(BaseResponseBean bean) {
                        ToastUtil.showShort("分配成功");
                        if (onCallBackListener != null) {
                            onCallBackListener.onCallBack();
                        }
                        dismiss();
                    }

                    @Override
                    public void onFailed(String msg, String code) {

                    }
                });
                break;
        }
    }

    public interface OnCallBackListener {
        void onCallBack();
    }
}

